package com.ruicar.afs.cloud.channel.affiliation.task;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.channel.affiliation.entity.ChannelAffiliatedUnitsTemp;
import com.ruicar.afs.cloud.channel.affiliation.service.ChannelAffiliatedUnitsTempService;
import com.ruicar.afs.cloud.channel.affiliation.utils.Common;
import com.ruicar.afs.cloud.channel.common.mq.sender.CaseAffiliationRelSender;
import com.ruicar.afs.cloud.channel.common.mq.sender.impl.CaseAffiliationRelSenderImpl;
import com.ruicar.afs.cloud.common.job.core.biz.model.ReturnT;
import com.ruicar.afs.cloud.common.job.core.handler.annotation.AfsJob;
import com.ruicar.afs.cloud.common.job.core.handler.annotation.AfsJobHandler;
import com.ruicar.afs.cloud.common.modules.dto.mq.affiliation.AffiliationRelInfo;
import com.ruicar.afs.cloud.common.mq.rabbit.message.AfsTransEntity;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author panhj
 */
@AfsJob
@AllArgsConstructor
@Slf4j
@Component
public class AffiliationTask {
    private static final Logger logger = LoggerFactory.getLogger(AffiliationTask.class);
    private ChannelAffiliatedUnitsTempService channelAffiliatedUnitsTempService;
    private final CaseAffiliationRelSenderImpl caseAffiliationRelSenderService;
    private final CaseAffiliationRelSender caseAffiliationRelSender;
    @Transactional(rollbackFor = Exception.class)
    @AfsJobHandler(value = "autoAffiliatedTask")
    public ReturnT<String> satrt(String params) {
        System.out.println("开始============================");
        List<ChannelAffiliatedUnitsTemp> list = channelAffiliatedUnitsTempService.list(Wrappers.<ChannelAffiliatedUnitsTemp>query().lambda().eq(ChannelAffiliatedUnitsTemp::getStatus, Common.STATUS_ENABLE));
        if (list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setMonthlySurplusNum(list.get(i).getMonthlyApplicationNum());
                list.get(i).setUpdateTime(new Date(System.currentTimeMillis()));
                channelAffiliatedUnitsTempService.updateById(list.get(i));
                AfsTransEntity<AffiliationRelInfo> transEntity = this.caseAffiliationRelSenderService.returnAffiliationRelInfo(list.get(i).getId().toString());
                log.info("挂靠及合作商信息……", transEntity);
                caseAffiliationRelSender.pushInfoToCaeAffiliation(transEntity);
            }
        }
        return ReturnT.SUCCESS;
    }
}
